package com.xhorse.lockplatform.manage.controller;

import com.xhorse.lockplatform.comm.constant.FilePathDefine;
import com.xhorse.lockplatform.comm.model.BaseQuery;
import com.xhorse.lockplatform.comm.mvc.AbstractListController;
import com.xhorse.lockplatform.comm.util.Paginator;
import com.xhorse.lockplatform.model.pojo.XhFeedBackPojo;
import com.xhorse.lockplatform.service.impl.FeedBackServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
@RequestMapping("FeedBack/feedbackResult")
public class FeedBackController extends AbstractListController<XhFeedBackPojo,
        FeedBackServiceImpl, FeedBackController.FeedBackInfoQuery> {

    @Resource
    public void setFeedBackServiceImpl(FeedBackServiceImpl feedBackService) {
        this.service = feedBackService;
    }

    public FeedBackController() {
        super("FeedBack/feedbackResult");
    }

    @Override
    protected boolean preList(int page, Paginator paginator, FeedBackInfoQuery query, Model model) {
        this.data = service.getFeedback(paginator, (byte) query.getSuccess(), query.getType());
        return super.preList(page, paginator, query, model);
    }

    /**
     * 查看上传的车钥匙图片
     *
     * @param response
     * @param request
     */
    @RequestMapping(value = "getImgByfilename", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getImgByfilename(HttpServletResponse response,
                                 HttpServletRequest request) {
        String imgpath = FilePathDefine.KEY_UPLOAD_PATH
                + request.getParameter("filename");
        // 设置header
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);
        response.setHeader("Content-disposition", "attachment;filename="
                + request.getParameter("filename") + "." + "png");// 设置头部信
        // 获取文件字节流
        File file = new File(imgpath);
        if (file.exists()) {
            OutputStream os = null;
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                os = response.getOutputStream();
                byte[] buffer = new byte[1024 * 1024];
                int index;
                while ((index = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, index);
                }
                os.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 查询参数bean
    public static class FeedBackInfoQuery extends BaseQuery {
        private int type;
        private String customerId;
        private String terminal;
        private int success;

        public void setType(int type) {
            this.type = type;
            this.addItem("type", type);
        }

        public void setCustomerId(String customerId) {
            this.customerId = customerId;
            this.addItem("customerId", customerId);
        }

        public void setTerminal(String terminal) {
            this.terminal = terminal;
            this.addItem("terminal", terminal);
        }

        public void setSuccess(int success) {
            this.success = success;
            this.addItem("success", success);
        }

        public int getType() {
            return type;
        }

        public String getCustomerId() {
            return customerId;
        }

        public String getTerminal() {
            return terminal;
        }

        public int getSuccess() {
            return success;
        }
    }
}
